﻿using CoffeeShop.Models.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace CoffeeShop.DAO
{
    public class OfficeDao
    {
        private CFMSEntities _entities = new CFMSEntities();

        /// <summary>
        /// Get all offices in Database
        /// </summary>
        /// <returns>List of Office</returns>
        public List<Office> GetAllOffices()
        {
            try
            {
                var resultQuery = from office in _entities.Offices
                                  select office;
                return resultQuery.ToList();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// Get all offices are activate
        /// </summary>
        /// <returns></returns>
        public List<Office> GetListActiveOffice()
        {
            try
            {
                var resultQuery = from office in _entities.Offices
                                  where office.IsDeleted == false
                                  select office;
                return resultQuery.ToList();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// Add new Office in to Database
        /// </summary>
        /// <param name="office">O</param>
        /// <returns>True: Successful - False: Fail</returns>
        public bool AddOffice(Office office)
        {
            try
            {
                _entities.Offices.Add(office);
                var result = _entities.SaveChanges();
                if (result > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Delete an office in to Database
        /// </summary>
        /// <param name="office">O</param>
        /// <returns>True: Successful - False: Fail</returns>
        public bool DeleteOffice(int officeId)
        {
            try
            {
                Office officeEnt = new Office();
                officeEnt = (from office in _entities.Offices
                                   where office.ID == officeId
                                         && office.IsDeleted == false
                                   select office).FirstOrDefault();

                // Set flag IsDeleted = true
                officeEnt.IsDeleted = true;
                _entities.Entry(officeEnt).State = EntityState.Modified;
                int resultQuery = _entities.SaveChanges();
                if (resultQuery > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Update an office
        /// </summary>
        /// <param name="office"></param>
        /// <returns></returns>
        public bool UpdateOffice(Office office)
        {
            try
            {
                _entities.Entry(office).State = EntityState.Modified;
                int resultQuery = _entities.SaveChanges();
                if (resultQuery > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Get an Office by Office's name
        /// </summary>
        /// <param name="officeName">Name of Office</param>
        /// <returns>An Office or null</returns>
        public Office GetOfficeByName(string officeName)
        {
            try
            {
                var resultQuery = (from office in _entities.Offices
                                   where office.Name == officeName
                                         && office.IsDeleted == false
                                   select office).FirstOrDefault();
                return resultQuery;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// Get an Office by Office's ID
        /// </summary>
        /// <param name="officeName">Name of Office</param>
        /// <returns>An Office or null</returns>
        public Office GetOfficeById(int officeId)
        {
            try
            {
                var resultQuery = (from office in _entities.Offices
                                   where office.ID == officeId
                                   select office).FirstOrDefault();
                return resultQuery;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// Check is exist an office which has the same name
        /// </summary>
        /// <param name="officeId"></param>
        /// <returns></returns>
        public bool IsExistedWhenUpdate(int officeId, string officeName)
        {
            try
            {
                var resultQuery = (from office in _entities.Offices
                                   where office.ID != officeId
                                         && office.Name == officeName
                                   select office).FirstOrDefault();
                if (resultQuery != null)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return true;
            }
        }
    }
}